<template>
  <div class="archives-container">
    <!-- 分类筛选栏 -->
    <div class="category-bar">
      <div
        v-for="(category, index) in categories"
        :key="category.id"
        class="category-card"
        @click="gotoCategories(category.value, category.label)"
      >
        <img :src="category.remark" class="card-bg" alt="分类背景" />
        <span class="category-name">{{ category.label }}</span>
      </div>
    </div>

    <!-- 博客列表 -->
    <div class="blog-list">
      <template v-for="(postsGroup, year) in grouped" :key="year">
        <h2 class="year-title">{{ year }}</h2>
        <div
          v-for="post in postsGroup"
          :key="post.id"
          class="post-card"
          @click="gotoDetail(post.id)"
        >
          <div class="post-image">
            <img :src="post.image" alt="文章封面" />
          </div>
          <div class="post-content">
            <h3 class="post-title">{{ post.title }}</h3>
            <p class="post-desc">{{ post.description }}</p>
            <div class="post-meta">
              <span class="post-date">{{ post.publishTime }}</span>
            </div>
          </div>
        </div>
      </template>
    </div>
  </div>
</template>

<script setup lang="ts">
import { useRouter } from "vue-router";
import { DictDataPageQuery, DictDataPageVO } from "@/api/system/dict-data";
import { ArchivesPageQuery, ArchivesPageVO } from "@/api/system/archives";
import BlogAPI from "@/api/blog/blog";
const router = useRouter();

// 博客文章类型编码 https://picsum.photos/1920/1080?random=1
const typeCode = "archives_type";

const queryCategoryParams = reactive<DictDataPageQuery>({
  pageNum: 1,
  pageSize: 10,
  dictCode: typeCode,
});

const queryArchivesParams = reactive<ArchivesPageQuery>({
  pageNum: 1,
  pageSize: 10,
});

const categories = ref<DictDataPageVO[]>();

const posts = ref<ArchivesPageVO[]>();
const grouped = ref();

// 加载博客文章列表
function loadPosts() {
  BlogAPI.getArchivesPage(queryArchivesParams).then((data) => {
    const sortedGroupted = Object.entries(
      data.list.reduce(
        (acc, post) => {
          const year = post?.publishTime.split("-")[0];
          if (!acc[year]) acc[year] = [];
          acc[year].push(post);
          return acc;
        },
        {} as Record<string, typeof posts.value>
      )
    ).sort((a, b) => a[0].localeCompare(b[0]));
    grouped.value = Object.fromEntries(sortedGroupted);
    posts.value = data.list;
  });
}
// 查询分类列表
function handleCategoryQuery() {
  BlogAPI.getCategoriesPage(queryCategoryParams).then((data) => {
    categories.value = data.list;
  });
}

function gotoCategories(type: string, label: string) {
  router.push("/blog/categories?type=" + type + "&label=" + label); // 跳转到 /categories 页面
}

function gotoDetail(id: Number) {
  router.push("/blog/detail?aid=" + id); // 跳转到 /detail 页面
}

onMounted(() => {
  handleCategoryQuery();
  loadPosts();
});
</script>

<style lang="scss" scoped>
.archives-container {
  background: #000;
  min-height: 100vh;
  padding: 80px 20px 40px; /* 顶部留出导航栏空间 */
}

.category-bar {
  max-width: 60%;
  margin: 0 auto 40px;
  display: flex;
  gap: 1.5rem;
  padding: 1rem;
  overflow-x: auto;
  scrollbar-width: thin;
  scrollbar-color: #333 #1a1a1a;

  .category-card {
    flex: 0 0 10rem;
    height: 6rem;
    border-radius: 0.5rem;
    position: relative;
    overflow: hidden;
    cursor: pointer;
    transition: transform 0.3s;

    &:hover {
      transform: scale(1.03);
    }

    .card-bg {
      width: 100%;
      height: 100%;
      object-fit: cover;
      filter: brightness(0.7);
    }

    .category-name {
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
      color: white;
      font-size: 1.2rem;
      text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.5);
      font-weight: 600;
    }
  }
}

.blog-list {
  max-width: 60%;
  margin: 0 auto;
}

.post-card {
  display: flex;
  height: 9rem;
  background: #1a1a1a;
  border-radius: 12px;
  margin-bottom: 30px;
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.3);
  transition: transform 0.3s;

  &:hover {
    transform: translateY(-5px);
  }

  .post-image {
    width: 35%;
    border-radius: 12px 0 0 12px;
    overflow: hidden;

    img {
      width: 100%;
      height: 100%;
      object-fit: cover;
    }
  }

  .post-content {
    width: 65%;
    padding: 25px;
    position: relative;
  }

  .post-title {
    color: #fff;
    font-size: 1.4rem;
    margin-bottom: 15px;
  }

  .post-desc {
    color: #ccc;
    font-size: 0.95rem;
    line-height: 1.6;
    margin-bottom: 30px;
  }

  .post-meta {
    position: absolute;
    bottom: 25px;
    right: 25px;

    .post-date {
      color: #888;
      font-size: 0.85rem;
    }
  }
}

// 新增年份标题样式
.year-title {
  color: #fff;
  font-size: 1.8rem;
  font-weight: 700;
  margin: 2rem 0 1rem;
  padding-left: 1rem;
  border-left: 4px solid #fff;
}
</style>
